//
//  UserDataManager.m
//  VISIKARD
//
//  Created by Phan Ba Minh on 6/5/12.
//  Copyright (c) 2012 kidbaw. All rights reserved.
//

#import "UserDataManager.h"
#import "Define.h"
#import "AutomationDataManager.h"

@implementation UserDataManager

#pragma mark - Encode - Decode

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInteger:self.userIsLogined forKey:STRING_CODER_USER_DATA_LOGIN_STATUS];
    [encoder encodeObject:self.userName forKey:STRING_CODER_USER_DATA_USER_NAME];
    [encoder encodeObject:self.userID forKey:STRING_CODER_USER_DATA_USER_ID];
    [encoder encodeObject:self.userSessionID forKey:STRING_CODER_USER_DATA_SESSION_ID];
	[encoder encodeObject:self.userPassword forKey:STRING_CODER_USER_DATA_PASSWORD];
    [encoder encodeInteger:self.userIsLoginedWith_FB_TW forKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW];
    [encoder encodeObject:self.userAvatarSmallURL forKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL];
    [encoder encodeObject:self.userFullName forKey:STRING_CODER_USER_DATA_FULL_NAME];
    [encoder encodeInteger:self.userProfileID forKey:STRING_CODER_USER_DATA_PROFILE_ID];
    [encoder encodeObject:self.userLocation forKey:STRING_CODER_USER_DATA_LOCATION];
    [encoder encodeObject:self.userPlacemark forKey:STRING_CODER_USER_DATA_PLACE_MARK];
    [encoder encodeObject:self.userEmail forKey:STRING_CODER_USER_DATA_EMAIL];
    [encoder encodeInteger:self.userRequestStatus forKey:STRING_CODER_USER_DATA_REQUEST_STATUS];
    
    [encoder encodeObject:self.userDeviceUDID forKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_UDID];
    [encoder encodeObject:self.userDeviceToken forKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_TOKEN];
    [encoder encodeObject:self.userRegisteredDeviceToken forKey:STRING_CODER_USER_DATA_USER_REGISTERED_DEVICE_TOKEN];
    [encoder encodeObject:self.userNotifyMe forKey:STRING_CODER_USER_DATA_USER_NOTIFY_ME];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_LOGIN_STATUS]) {
            self.userIsLogined = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_LOGIN_STATUS];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NAME]) {
            self.userName = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NAME];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_ID]) {
            self.userID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_ID];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_SESSION_ID]) {
            self.userSessionID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_SESSION_ID];
        }
		if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_PASSWORD]) {
            self.userPassword = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_PASSWORD];
        }
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW]) {
            self.userIsLoginedWith_FB_TW = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_LOGIN_WITH_FB_TW];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL]) {
            self.userAvatarSmallURL = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_AVATAR_SMALL_URL];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_FULL_NAME]) {
            self.userFullName = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_FULL_NAME];
        }
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_PROFILE_ID]) {
            self.userProfileID = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_PROFILE_ID];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_PLACE_MARK]) {
            self.userPlacemark = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_PLACE_MARK];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_EMAIL]) {
            self.userEmail = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_EMAIL];
        }
        if ([decoder decodeIntegerForKey:STRING_CODER_USER_DATA_REQUEST_STATUS]) {
            self.userRequestStatus = [decoder decodeIntegerForKey:STRING_CODER_USER_DATA_REQUEST_STATUS];
        }
        
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_UDID]) {
            self.userDeviceUDID = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_UDID];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_TOKEN]) {
            self.userDeviceToken = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_UPDATED_DEVICE_TOKEN];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_REGISTERED_DEVICE_TOKEN]) {
            self.userRegisteredDeviceToken = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_REGISTERED_DEVICE_TOKEN];
        }
        if ([decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NOTIFY_ME]) {
            self.userNotifyMe = [decoder decodeObjectForKey:STRING_CODER_USER_DATA_USER_NOTIFY_ME];
        }
    }
    return self;
}

//- (void)setUserAvatarSmallURL:url {
//    SDWebImageManager *manager = [SDWebImageManager sharedManager];
//    [manager downloadWithURL:[NSURL URLWithString:url] delegate:self];
//    _userAvatarSmallURL = url;
//}

#pragma mark - SDWebImageDownloaderDelegate

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
    //    NSLog(@"PhotoShopObject; webImageManager; didFinishWithImage");
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error {
    //    NSLog(@"PhotoShopObject; webImageManager; didFailWithError");
}

#pragma mark - Method

- (void)cleanUserCacheAndCookie { 
    NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [cookies deleteCookie:cookie];
    }
    // ************************* //
}

- (void)resetAllData {
    // reset profile data
    [UserDataManager Shared].userID = @"";
    [UserDataManager Shared].userSessionID = @"";
    [UserDataManager Shared].userName = @"";
    [UserDataManager Shared].userIsLogined = 0;
    [UserDataManager Shared].userIsLoginedWith_FB_TW = 0;
    [UserDataManager Shared].userAvatarSmallURL = @"";
    [UserDataManager Shared].userFullName = @"";
    [UserDataManager Shared].userEmail = @"";
    [UserDataManager Shared].userRequestStatus = 0;
    
    [self cleanUserCacheAndCookie];
}

- (NSString*)getUserPlacemark {
    [[AutomationDataManager Shared] startUpdateLocationService];
    if (self.userPlacemark.locality == nil) {
        return self.userPlacemark.administrativeArea;
    }
    return self.userPlacemark.locality;
}

- (void)save {
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self] forKey:STRING_USER_DEFAULT_USER_DATA_MANAGER];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

static UserDataManager *_userDataManager;
+ (UserDataManager *)Shared
{
    if (!_userDataManager) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:STRING_USER_DEFAULT_USER_DATA_MANAGER];
        if ([NSKeyedUnarchiver unarchiveObjectWithData:data]) {
            _userDataManager = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        }
        
        if (_userDataManager == nil) {
            _userDataManager = [UserDataManager new];
        }
    }
    return _userDataManager;
}

@end
